package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;
import cn.mxz.robots.util.RobotsUtlis;

/**
 * 河南省
 * 排名:完成
 * 去年排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-24
 */
public class Henan extends Exam implements IRank
{

	public static void main(String[] args)
	{
		Henan henan = new Henan(new Student("15410516110064", "", "410725199711212425", "", BEN_KE_LI_KE));

		//System.out.println(henan.login(""));
		henan.lastYearRank();
	}

	public Henan(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{

		Request loginRequest = new Request("http://www.heao.gov.cn/datacenter/pages/PZCJQuery.aspx", mSha1);
		Document initDoc = Jsoup.parse(curlGet(loginRequest));

		loginRequest.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36");
		loginRequest.addParams("__VIEWSTATE", RobotsUtlis.urlEncode(initDoc.select("input[name=__VIEWSTATE]").val()));
		loginRequest.addParams("__VIEWSTATEGENERATOR", RobotsUtlis.urlEncode(initDoc.select("input[name=__VIEWSTATEGENERATOR]").val()));
		loginRequest.addParams("__EVENTVALIDATION", RobotsUtlis.urlEncode(initDoc.select("input[name=__EVENTVALIDATION]").val()));
		loginRequest.addParams("txtZKZH", mStudent.getStudentId());
		loginRequest.addParams("txtSFZH", mStudent.getIdCard());
		loginRequest.addParams("txtBMXH", "");
		loginRequest.addParams("SubButton.x", "55");
		loginRequest.addParams("SubButton.y", "7");

		String loginHtml = curlPost(loginRequest);

		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("tdnote"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("ErrorMSG"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Elements trs = Jsoup.parse(parseHtml).select(".result table tr");
		JSONObject data = new JSONObject();

		data.put("exam_id", trs.get(1).select(".common").text().replace(" ", ""));
		data.put("name", trs.get(3).select(".common").text().replace(" ", ""));
		data.put("ranking", getRank(11+31, trs.get(8).select(".common").text().replace(" ", ""), mStudent.getType()));
		data.put("province", "河南省");

		JSONObject score = new JSONObject();
		score.put("语文", trs.get(4).select(".common").text().replace(" ", ""));
		score.put("数学", trs.get(5).select(".common").text().replace(" ", ""));
		score.put("外语", trs.get(6).select(".common").text().replace(" ", ""));
		score.put("综合", trs.get(7).select(".common").text().replace(" ", ""));
		score.put("总分", trs.get(8).select(".common").text().replace(" ", ""));
		score.put("听力", trs.get(9).select(".common").text().replace(" ", ""));

		data.put("score", score.toString());

		return data.toString();
	}

	@Override
	public void initRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(11 + 31);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2015/124198.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2015/124198_3.html"));
		// 文科 http://www.gxeduw.com/news/2015/124062.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 307951;
		int asSize = as.size();
		for (int i = 3; i < asSize; i++)
		{
			String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			//String countString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");

			
			Integer ranking = pre + 1;
			
			Integer count = Integer.parseInt(leiji)-pre;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_LI_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + count + "rank: " + ranking);
			System.out.println(rank);
		}
	}

	@Override
	public void lastYearRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(11);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2014/107185_3.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2014/107186_3.html"));
		// 文科 http://www.gxeduw.com/news/2014/107186.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 184231;
		int asSize = as.size();
		for (int i = 1; i < asSize; i++)
		{
			String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			//String countString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");

			if(scoreString.equals("")||leiji.equals(""))continue;
			Integer ranking = pre + 1;
			
			Integer count = Integer.parseInt(leiji)-pre;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_WEN_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + count + "rank: " + ranking);
			System.out.println(rank);
		}
	}
}
